package com.demo.web;

import com.demo.entity.Student;
import com.demo.service.StudentService;
import com.demo.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wzm
 * @create 2023-06-29 9:46
 */
@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();

    //模糊查询的方法
    protected void getLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置请求和响应的编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //2.获取请求的参数
        String likeName = req.getParameter("sera");

        //3.将参数封装为对象
        Student student = new Student();
        student.setUsername(likeName);

        //4.调用service对应的方法
        List<Student> byLike = studentService.findByLike(student);

        //5.判断结果进行处理
        if(byLike == null){
            resp.getWriter().write("未查询到数据");
            return;
        }

        //6.响应内容
        req.setAttribute("stuList",byLike);

        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }
}
